package com.practices2.activity;

import java.security.cert.LDAPCertStoreParameters;

import android.content.ComponentName;
import android.content.Context;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.practices2.R;
import com.practices2.adapter.DownloadListAdapter;
import com.practices2.base.BaseActivity;
import com.practices2.testdata.DownloadUrls;
import com.practices2.util.download.DownloadManager;
import com.practices2.util.download.IDownloadService;

public class DownloadAcitivity extends BaseActivity {

	private ListView downloadlist;
	private Button btn_add;

	private IDownloadService downloadService;
	private DownloadManager downloadManager;
	private DownloadListAdapter downloadListAdapter;

	private int index = 0;
	ServiceConnection service = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			downloadService = (IDownloadService) service;
		}
	};

	// private downl

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.activity_download);
	}

	@Override
	protected void onAfterOnCreate(Bundle savedInstanceState) {
		super.onAfterOnCreate(savedInstanceState);
		setTitle(R.string.title_download_activity);
		downloadManager = DownloadManager.getDownloadManager();
		setContentView(R.layout.activity_download);
	}

	@Override
	protected void onAfterSetContentView() {
		super.onAfterSetContentView();
		
		downloadlist = (ListView) findViewById(R.id.lv_downlist);
		btn_add = (Button) findViewById(R.id.btn_add);
		downloadListAdapter=new DownloadListAdapter(DownloadAcitivity.this, downloadlist);
		downloadlist.setAdapter(downloadListAdapter);
		

		btn_add.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				downloadManager.addHandler(DownloadUrls.url[index]);
				downloadListAdapter.addItem(DownloadUrls.url[index]);
				index++;
				if (index >= DownloadUrls.url.length) {
					index = 0;
				}
			}
		});

	}

}
